public record MedalCount(int gold, int silver, int bronze)Tour d’horizon et cas d’utilisation des records
JD4 14 : JEP 359 Records (Preview)
JDK 15 : JEP 384 Records (Second Preview)
JDK 16 : JEP 395: Records
public record MedalCount(int gold, int silver, int bronze)public record MedalCount(int gold, int silver, int bronze)
var medalCount = new MedalCount(1, 2, 3);
var gold = medalCount.gold;
var silver = medalCount.silver;
var bronze = medalCount.bronze;public class MedalCount {
private final int gold;
private final int silver;
private final int bronze;
public MedalCount(int gold, int silver, int bronze) {
this.gold = gold;
this.silver = silver;
this.bronze = bronze;
}
....
}public record MedalCount(int gold, int silver, int bronze)public class MedalCount {
private final int gold;
private final int silver;
private final int bronze;
....
@Override
public String toString() {
return "MedalCount[gold=" + gold
+ ", silver=" + silver
+ ", bronze=" + bronze + "]";
}
}public record MedalCount(int gold, int silver, int bronze)public class MedalCount {
private final int gold;
private final int silver;
private final int bronze;
....
@Override
public int hashCode() {
return Objects.hash(gold, silver, bronze);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof MedalCount)) {
return false;
} else {
MedalCount other = (MedalCount) obj;
return Objects.equals(gold, other.gold)
&& Objects.equals(silver, other.silver)
&& Objects.equals(bronze, other.bronze);
}
}
}binout
@Data
public class MedalCount {
private final int gold;
private final int silver;
private final int bronze;
}
MedalCount medalCount = new MedalCount(1, 2, 3);
int gold = medalCount.getGold();
int silver = medalCount.getSilver();
int bronze = medalCount.getBronze();🔗 Dépendance nécessaire
🪄 Configuration Annotation Processor
🤯 Ouvre la porte à d’autres fonctionnalités
case class MedalCount(gold: Int, silver: Int, bronze: Int)data class MedalCount(gold: Int, silver: Int, bronze: Int)@dataclass(Frozen=True)
class MedalCount:
gold: int
silver: int
bronze: intObjet de transport de donnée pour faciliter la sérialisation/désérialisation
Facilité d’écriture avec les records
Les records comme clé composée pour une map
var countryPerMedals =
new HashMap<MedalCount, List<String>>();
countryPerMedals.put(
new MedalCount(5, 2, 1), asList("us", "cn"));
countryPerMedals.put(
new MedalCount(1, 1, 1), asList("fr", "de"));public List<String> sortCountryByMedal(List<String> countries) {
record Data(String country, MedalCount medalCount){}
return countries.stream()
.map(country ->
new Data(country, getMedalCount(country)))
.sorted(Comparator.comparing(d -> d.medalCount))
.map(Data::country)
.collect(toList());
}Utilisation de types primitifs pour modéliser des "petits" objets
public MedalCount getMedalCount(String country,
String olympicGame,
String sport) {
....
}
var medalCount = getMedalCount("FR", "14", "swimming"); ✅
var medalCount = getMedalCount("14", "FR", "swimming"); 🤯Identifier Type pattern
public record CountryCode(String value)
public record OlympicGameId(String value)
public record SportName(String value)
public MedalCount getMedalCount(CountryCode countryCode,
OlympicGameId olympicGameId,
SportName sportName) {
....
}
var medalCount = getMedalCount(CountryCode("FR"),
OlympicGameId("14"),
SportName("swimming"));Validation post contruction pour assurer des invariants métier
public record MedalCount(int gold, int silver, int bronze) {
public MedalCount {
if (gold < 0 || silver < 0 || bronze < 0) {
throw new IllegalArgumentException(
"Medal count should be positive")
}
}
}Entity: objet métier avec une identité et un cycle de vie
Value Object: objet métier immutable et défini par ses attributs
🎉 Les records, solution idéale pour modéliser les Value Objects ! 🎉
public enum Unit { m, km }
public record Distance(long value, Unit unit)
var d1 = new Distance(100, m)
var d2 = new Distance(400, m)
var d3 = new Distance(42, km)public enum Unit { m(1), km(1000);
final int valueInMeter;
Unit(int valueInMeter) {
this.valueInMeter = valueInMeter;
}
}
public record Distance(long value, Unit unit) {
Distance asMeter() {
return new Distance(
this.value * this.unit.valueInMeter, m)
}
public Distance add(Distance other) {
return new Distance(
this.asMeter().value + other.asMeter().value, m)
}
}TODO
TODO
TODO
TODO
record Point(int x, int y) {}
void printSum(Object o) {
if (o instanceof Point(int x, int y)) {
System.out.println(x+y);
}
}TODO